import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.ParameterMetaData;
import java.util.Scanner;

public class MyTelnetClient {
    private Socket socket = null;
    public MyTelnetClient(String serverIP,int serverPOrt) throws IOException {
        socket = new Socket(serverIP,serverPOrt);
    }
    public void run() {
        System.out.println("客户端启动！");
        Scanner scanner = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()){
            PrintWriter printWriter = new PrintWriter(outputStream);
            Scanner sca = new Scanner(inputStream);
            while(true) {
                System.out.print("用户名：");
                String name = scanner.next();
                printWriter.println(name);
                printWriter.flush();
                System.out.print("密码：");
                String password = scanner.next();
                printWriter.println(name);
                printWriter.flush();
                String cmd = scanner.next();
                if (cmd.equals("TRUE")) {
                    while (cmd.equals("TRUE")) {
                        System.out.print("输入发送请求>:");
                        String request = scanner.next();
                        if (request.equals("EXIT")) {
                            System.out.println("结束请求");
                            break;
                        }
                        printWriter.println(request);
                        printWriter.flush();
                        String reponse = sca.next();
                        System.out.println(reponse);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        MyTelnetClient myTelnetClient = new MyTelnetClient("127.0.0.1",9090);
        myTelnetClient.run();
    }
}
